﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Space
{
    public class Laser : Weapon
    {
        float lastAngle;

        public float LifeTime { get; set; }
        float lifeTimer;

        float FadeTime { get { return Math.Min(0.5f, LifeTime / 2); } }

        public float MaxThickness { get; set; }

        public float Range { get; set; }

        public Laser(Ship ship)
            : base(ship)
        {
            Alive = true;
        }

        public override void Update(float time)
        {
            Position = Ship.Position;

            if (lifeTimer < LifeTime)
            {
                lifeTimer += time;
                if (lifeTimer > LifeTime) lifeTimer = LifeTime;
            }
            else
            {
                Alive = false;
            }

            if (Target != null)
            {
                Target.TakeDamage(time * Damage);
                if (!Target.Alive)
                {
                    lifeTimer = Math.Max(lifeTimer, LifeTime - FadeTime);
                }
            }

            base.Update(time);
        }

        public override void Draw()
        {
            ICamera camera = Game.Services.GetService<ICamera>();

            Model model = Game.Content.Load<Model>("models/laser");
            float length;
            float angle;

            if (Target == null)
            {
                length = Range;
                angle = Ship.Angle;
            }
            else
            {
                if (!Target.Alive) length = Range;
                else length = (Ship.Position - Target.Position).Length();
                if (!Target.Alive) angle = lastAngle;
                else angle = lastAngle = (float)Math.Atan2(Target.Position.Z - Ship.Position.Z, Target.Position.X - Ship.Position.X);
            }

            //model.Draw(Matrix.CreateScale(length, 1, 1) * Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(Ship.Position), camera.View, camera.Projection);

            float thickness;
            if (lifeTimer > FadeTime && lifeTimer < LifeTime - FadeTime)
                thickness = MaxThickness;
            else
            {
                if (lifeTimer < LifeTime / 2)
                    thickness = lifeTimer / FadeTime * MaxThickness;
                else
                    thickness = (LifeTime - lifeTimer) / FadeTime * MaxThickness;
            }

            foreach (ModelMesh m in model.Meshes)
            {
                foreach (Effect effect in m.Effects)
                {
                    effect.CurrentTechnique = effect.Techniques["Technique1"];
                    effect.Parameters["World"].SetValue(Matrix.CreateScale(thickness, thickness, -length) * Matrix.CreateRotationY(MathHelper.PiOver2 - angle) * Matrix.CreateTranslation(Ship.Position));
                    effect.Parameters["Projection"].SetValue(camera.Projection);
                    effect.Parameters["View"].SetValue(camera.View);
                    effect.Parameters["EmissiveColor"].SetValue(Color.Black.ToVector3());
                    effect.Parameters["DiffuseColor"].SetValue(Color.Red.ToVector3());
                    effect.Parameters["Light1Direction"].SetValue(Vector3.Down);
                }
                m.Draw();
            }
        }
    }
}
